home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 5 / Gold Medal Software - Volume 5 (Gold Medal) (1995).iso / database / cff51b.arj / FILETEST.C < prev    next >
C/C++ Source or Header  |  1993-10-22  |  9KB  |  382 lines

  1. /* FILETEST.C */
  2. /* Copyright 1990, 1991, 1992, 1993 Norman D. Culver Ft. Lauderdale, FL */
  3. /*                    All Rights Reserved                                   */
  4.  
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "../cff.h"
  8.  
  9. #define CLOCKS_PER_SECOND 1000000L
  10.  
  11. /* TEST CHOICES */
  12. #define PFLAGS 0
  13. #define MEMFILE 1
  14. #define EXTDMEM 1
  15. #define CFFFILE 1
  16. #define EXTERNAL 1
  17.  
  18. extern int errno;
  19. extern long clock();
  20. extern int rand();
  21.  
  22.  
  23. static void file_test(void *hd, void *hd1);
  24. static void big_test(void *hd, void *hd1);
  25.  
  26. void
  27. main()
  28. {
  29. void *hxxx;
  30. void *huniq;
  31. void *hcfile;
  32. void *hsxxx;
  33. void *hsxxx1;
  34. void *hcxxx;
  35. void *hxfile;
  36.  
  37.     cfport_settestflags(1);
  38.     cfinit("filetest",256,NULL);
  39.     cfprintf("HASHED DIRECTORY -- test of file property 4bytes per i/o\n");
  40. #if MEMFILE
  41.     cfprintf("MEMORY FILE\n");
  42.     hxxx = cfopen("MEMORY/XXX",F_RDWR|F_CREAT,NULL);
  43.     if(!hxxx) {
  44.         cfprintf("ERROR: create XXX err=%d\n", errno);
  45.         exit(0);
  46.     }
  47.     huniq = cfopen("MEMORY/XXX",F_RDWR|F_CREAT|F_TEMP|F_UNIQ|F_FILEONLY, NULL);
  48.     if(!huniq) {
  49.         cfprintf("ERROR: create UNIQUE TEMP FILE err=%d\n", errno);
  50.         exit(0);
  51.     }
  52. #if PFLAGS
  53.     cfpflags("hxxx", hxxx);
  54.     cfpflags("huniq", huniq);
  55. #endif
  56.     file_test(huniq, NULL);
  57.     big_test(huniq, NULL);
  58. cfprintf("NOW CLOSING\n");
  59.     cfclose(huniq);
  60. #endif /* MEMFILE */
  61.  
  62. #if EXTDMEM
  63.     cfprintf("EXTDMEM FILE\n");
  64.     hsxxx = cfopen("EXTDMEM/XXX", F_RDWR|F_CREAT|F_FILEONLY, NULL);
  65.     if(!hsxxx) {
  66.         cfprintf("ERROR: create EXTDMEM/XXX err=%d\n", errno);
  67.         exit(0);
  68.     }
  69.     {/* Special case file, with preallocation of local bitmap */
  70.     OPNINFO info;
  71.  
  72.         info.initial_entries = 0;
  73.         info.bitmap_prealloc = 240000;
  74.         info.data_prealloc = 0;
  75.         hsxxx1 = cfopen(    "EXTDMEM/XXX1",
  76.                             F_RDWR|F_CREAT|F_BITMAP|F_FILEONLY|F_TEMP,
  77.                             &info);
  78.         if(!hsxxx1) {
  79.             cfprintf("ERROR: create EXTDMEM/XXX1 err=%d\n", errno);
  80.             exit(0);
  81.         }
  82.     }
  83. #if PFLAGS
  84.     cfpflags("hsxxx", hsxxx);
  85.     cfpflags("hsxxx1", hsxxx1);
  86. #endif
  87.     file_test(hsxxx, hsxxx1);
  88.     big_test(hsxxx1, NULL);
  89. cfprintf("NOW CLOSING\n");
  90.     cfclose(hsxxx1);
  91.     cfclose(hsxxx);
  92.  
  93.     cfprintf("REOPEN EXTDMEM FILE\n");    
  94.     hsxxx = cfopen("EXTDMEM/XXX", F_RDWR, NULL);
  95.     if(!hsxxx) {
  96.         cfprintf("ERROR: reopen EXTDMEM:/XXX err=%d\n", errno);
  97.         exit(0);
  98.     }
  99.     file_test(hsxxx,NULL);
  100.     big_test(hsxxx,NULL);
  101.     cfunlink(hsxxx, NULL);
  102. #endif /* EXTDMEM */
  103.  
  104. #if CFFFILE
  105.     cfprintf("CFF DISK FILE\n");
  106.     hcfile = cfopen("testfile.cff", F_RDWR|F_CREAT, NULL);
  107.     if(!hcfile) {    
  108.         cfprintf("ERROR: create testfile.cff err=%d\n", errno);
  109.         exit(0);
  110.     }
  111.     hcxxx = cfopen("testfile.cff/XXX",
  112.             F_RDWR|F_CREAT|F_FILEONLY, NULL);
  113.     if(!hcxxx) {    
  114.         cfprintf("ERROR: create testfile.cff/XXX err=%d\n", errno);
  115.         exit(0);
  116.     }
  117. #if PFLAGS
  118.     cfpflags("hcfile", hcfile);
  119.     cfpflags("hcxxx", hcxxx);
  120. #endif
  121.     file_test(hcxxx, NULL);
  122. cfprintf("NOW CLOSING\n");
  123.     cfclose(hcxxx);
  124.     cfclose(hcfile);
  125.  
  126.     cfprintf("REOPEN CFF DISK FILE\n");    
  127.     hcfile = cfopen("testfile.cff", F_RDWR, NULL);
  128.     if(!hcfile) {    
  129.         cfprintf("ERROR: reopen testfile.cff err=%d\n", errno);
  130.         exit(0);
  131.     }
  132.     hcxxx = cfopen("testfile.cff/XXX", F_RDWR, NULL);
  133.     if(!hcxxx) {    
  134.         cfprintf("ERROR: reopen testfile.cff/XXX err=%d\n", errno);
  135.         exit(0);
  136.     }
  137.     file_test(hcxxx,NULL);
  138.     big_test(hcxxx,NULL);
  139. cfprintf("NOW CLOSING\n");
  140.     cfclose(hcxxx);
  141.     cfclose(hcfile);
  142.     cfunlink("testfile.cff");
  143. #endif /* CFF FILE */
  144.  
  145. #if EXTERNAL
  146.     cfprintf("EXTERNAL DISK FILE\n");
  147.     hxfile = cfopen("testfile.tst", F_RDWR|F_CREAT, NULL);
  148.     if(!hxfile) {    
  149.         cfprintf("ERROR: create EXTERNAL FILE err=%d\n", errno);
  150.         exit(0);
  151.     }
  152. #if PFLAGS
  153.     cfpflags("hxfile", hxfile);
  154. #endif
  155.     file_test(hxfile,NULL);
  156.     cfclose(hxfile);
  157.  
  158.     cfprintf("REOPEN EXTERNAL DISK FILE\n");    
  159.     hxfile = cfopen("testfile.tst", F_RDWR, NULL);
  160.     if(!hxfile) {    
  161.         cfprintf("ERROR: reopen EXTERNAL FILE err=%d\n", errno);
  162.         exit(0);
  163.     }
  164.     file_test(hxfile,NULL);
  165.     cfclose(hxfile);
  166.     cfunlink("testfile.tst");
  167. #endif /* EXTERNAL */
  168.  
  169.     cfexit();
  170. }
  171. static void
  172. file_test(void *hd, void *hd1)
  173. {
  174. long i, start, end, diff, err;
  175. long amount;
  176. long obtype = cfobtype(hd);
  177. int nfile = (hd1) ? 2:1;
  178.  
  179.         if(obtype & OB_MEM)
  180.             amount = 250000;
  181.         else if(obtype & OB_CFILE)
  182.             amount = 100000;
  183.         else if(obtype & OB_XFILE)
  184.             amount = 10000;
  185.         else if(obtype & OB_SMEM)
  186.             amount = 150000;
  187.         else {
  188.             cfprintf("UNKNOWN OBTYPE: %x\n", obtype);
  189.             return;
  190.         }
  191.  
  192.         /* WRITE DATA */
  193.         start = clock();
  194.         if(cfisnew(hd)) {
  195.             cfseek(hd, 0, S_SET);
  196.             if(hd1)
  197.                 cfseek(hd1, 0, S_SET);
  198.             for(i = 0; i < amount; ++i) {
  199.                 if((err = cfwrite(hd, &i, 4)) != 4) {
  200.                     cfprintf("  File write failed at i=%d err=%d\n", i, err);
  201.                     break;
  202.                 }
  203.                 /* SECOND FILE, forces multiple noncontiguous chunks into
  204.                     both file maps */
  205.                 if(hd1) if((err = cfwrite(hd1, &i, 4)) != 4) {
  206.                     cfprintf("  File write1 failed at i=%d err=%d\n", i, err);
  207.                     break;
  208.                 }
  209.             }
  210.             end = clock();
  211.             diff = end - start;
  212.             diff /= CLOCKS_PER_SECOND/10;
  213.             if(diff == 0) diff = 1;
  214.             cfprintf("  FILEWRITES PER SEC = %ld\n", (nfile*i*10) / diff);
  215.         }
  216. cfprintf("  Bytesalloc=%lu Bytesused=%lu\n  File1 size=%lu File1 alloc=%lu\n",
  217. cfbytesalloc(hd), cfbytesused(hd), cffilesize(hd), cffilealloc(hd));
  218. if(nfile == 2)
  219. cfprintf("  File2 size=%lu File2 alloc=%lu\n", cffilesize(hd1), cffilealloc(hd1));
  220.  
  221.         /* READ FORWARD */
  222.         start = clock();
  223.         cfseek(hd, 0, S_SET);
  224.         for(i = 0; i < amount; ++i) {
  225.         long val;
  226.             if((err = cfread(hd, &val, 4)) != 4) {
  227.                 cfprintf("  File read failed at i=%ld err=%ld val=%ld\n", i, err,val);
  228.                 break;
  229.             }
  230.             if(val != i) {
  231.                 cfprintf("  File read bad value=%d should be %d\n", val, i);
  232.                 break;
  233.             }
  234.         }
  235.         end = clock();
  236.         diff = end - start;
  237.         diff /= CLOCKS_PER_SECOND/10;
  238.         if(diff == 0) diff = 1;
  239.         cfprintf("  FILEREADS PER SEC = %ld\n", (i*10) / diff);
  240.  
  241.         /* READ BACKWARD */
  242.         start = clock();
  243.         for(i = amount-1; i >= 0; --i) {
  244.         long val;
  245.             cfseek(hd, i*4, S_SET);
  246.             if((err = cfread(hd, &val, 4)) != 4) {
  247.                 cfprintf("  File backward read failed at i=%d err=%d\n", i, err);
  248.                 break;
  249.             }
  250.             if(val != i) {
  251.                 cfprintf("  File backward read bad value=%d should be %d\n", val, i);
  252.                 break;
  253.             }
  254.         }
  255.         end = clock();
  256.         diff = end - start;
  257.         diff /= CLOCKS_PER_SECOND/10;
  258.         if(diff == 0) diff = 1;
  259.         cfprintf("  BACKWARD READS PER SEC = %ld\n", ((amount -i)*10) / diff);
  260.  
  261.         /* RANDOM */
  262.         start = clock();
  263.         for(i = 0; i < amount/10; ++i) {
  264.         long val, j;
  265.  
  266.             j = rand() % amount;
  267.             cfseek(hd, j*4, S_SET);
  268.             if((err = cfread(hd, &val, 4)) != 4) {
  269.                 cfprintf("  File random read failed at j=%d err=%d\n", j, err);
  270.                 break;
  271.             }
  272.             if(val != j) {
  273.                 cfprintf("  File random read bad value=%d should be %d\n", val, j);
  274.                 break;
  275.             }
  276.         }
  277.         end = clock();
  278.         diff = end - start;
  279.         diff /= CLOCKS_PER_SECOND/10;
  280.         if(diff == 0) diff = 1;
  281.         cfprintf("  RANDOM READS PER SEC = %ld\n", (i*10) / diff);
  282.  
  283.     /* CLIP THE FILESIZE SUBSTANTIALLY */
  284.     if(!cfisnew(hd))
  285.     {
  286.         amount /= 2;
  287.  
  288.         /* TRUNCATE AND REREAD FILE */
  289.         cfprintf("   TRUNCATE AND REREAD FILE\n");
  290.  
  291.         cftruncate(hd, amount * 4);
  292.  
  293.         start = clock();
  294.         cfseek(hd, 0, S_SET);
  295.         for(i = 0; i < amount; ++i) {
  296.         long val;
  297.             if((err = cfread(hd, &val, 4)) != 4) {
  298.                 cfprintf("  File read failed at i=%ld err=%ld val=%ld\n", i, err,val);
  299.                 break;
  300.             }
  301.             if(val != i) {
  302.                 cfprintf("  File read bad value=%d should be %d\n", val, i);
  303.                 break;
  304.             }
  305.         }
  306.         end = clock();
  307.         diff = end - start;
  308.         diff /= CLOCKS_PER_SECOND/10;
  309.         if(diff == 0) diff = 1;
  310.         cfprintf("  FILEREREADS PER SEC = %ld\n", (i*10) / diff);
  311. cfprintf("  Bytesalloc=%lu Bytesused=%lu\n  File1 size=%lu File1 alloc=%lu\n",
  312. cfbytesalloc(hd), cfbytesused(hd), cffilesize(hd), cffilealloc(hd));
  313.  
  314.     }
  315.  
  316. }
  317. static void
  318. big_test(void *hd, void *hd1)
  319. {
  320. #define BUFSIZE 15000
  321. long buf[BUFSIZE];
  322. int i,j;
  323. long err;
  324.     cfprintf("  TEST LARGE BUFFER READ/WRITE\n");
  325.  
  326.  
  327.     cfseek(hd, 0, S_SET);
  328.     if(hd1)
  329.         cfseek(hd1, 0, S_SET);
  330.  
  331.     cfprintf("  WRITE 10 BUFFERS of %lu bytes each\n", sizeof(buf));
  332.     for(i = 0; i < 10; ++i)
  333.     {
  334.         for(j = 0; j < BUFSIZE-1; ++j)
  335.             buf[j] = (j<<8)+i;
  336.         if((err = cfwrite(hd, buf, sizeof(buf))) != sizeof(buf)) {
  337.             cfprintf("BIG write error hd i=%d err=%d\n", i, err);
  338.         }
  339.         if(hd1)
  340.             if((err = cfwrite(hd1, buf, sizeof(buf))) != sizeof(buf)) {
  341.                 cfprintf("BIG write error hd1 i=%d err=%d\n", i, err);
  342.             }
  343.     }
  344.     cfprintf("  WRITE DONE\n");
  345.  
  346.     cfseek(hd, 0, S_SET);
  347.     if(hd1)
  348.         cfseek(hd1, 0, S_SET);
  349.     cfprintf("  READ AND CHECK BUFFERS\n");
  350.     for(i = 0; i < 10; ++i)
  351.     {
  352.         memset(buf, 0, sizeof(buf));
  353.         if((err = cfread(hd, buf, sizeof(buf))) != sizeof(buf)) {
  354.             cfprintf("BIG READ ERROR hd i=%d err=%d\n", i, err);
  355.             continue;
  356.         }
  357.         for(j = 0; j < BUFSIZE-1; ++j) {
  358.             if(buf[j] != (j<<8)+i) {
  359.                 cfprintf("BAD DATA hd %x should be %x at i=%d j=%d\n",
  360.                     buf[j], (j<<8)+i, i, j);    
  361.                 break;
  362.             }
  363.         }
  364.         if(hd1) {
  365.             memset(buf, 0, sizeof(buf));
  366.             if((err = cfread(hd1, buf, sizeof(buf))) != sizeof(buf)) {
  367.                 cfprintf("BIG READ ERROR hd1 i=%d err=%d\n", i, err);
  368.                 continue;
  369.             }
  370.             for(j = 0; j < BUFSIZE-1; ++j) {
  371.                 if(buf[j] != (j<<8)+i) {
  372.                     cfprintf("BAD DATA hd %x should be %x at i=%d j=%d\n",
  373.                         buf[j], (j<<8)+i, i, j);    
  374.                     break;
  375.                 }
  376.             }
  377.         }
  378.     }
  379.     cfprintf("  READ AND CHECK DONE\n");
  380. }
  381.  
  382.